<%= f.input :reimbursement_id, as: :select, collection: Reimbursement.all.map { |r| [r.id, r.id] } %>
<%= f.input :status, as: :select, collection: WorkOrder::STATUSES %>

<% unless f.object.new_record? %>
  <li class="string input optional">
    <label class="label">状态</label>
    <span><%= f.object.status %></span>
  </li>
<% end %>

<% if f.object.is_a?(AuditWorkOrder) || f.object.is_a?(CommunicationWorkOrder) %>
  <%= f.input :problem_type_id, as: :select, collection: ProblemType.all.map { |pt| [pt.name, pt.id] }, include_blank: '无', input_html: { class: 'problem-type-select' } %>
  <%= f.input :problem_description_id, as: :select, collection: [], include_blank: '无', input_html: { class: 'problem-description-select' } %>
  <div class="input optional">
    <label class="label">补充材料</label>
    <select class="material-select" multiple style="width: 100%; min-height: 60px;"></select>
  </div>
<% end %>

<% if f.object.is_a?(AuditWorkOrder) %>
  <%= f.input :processing_opinion, as: :select, collection: ProcessingOpinionOptions.all, include_blank: '无' %>
  <% if !f.object.new_record? && f.object.audit_result.present? %>
    <%= f.input :audit_result, input_html: { disabled: true } %>
    <%= f.input :audit_comment %>
    <%= f.input :audit_date, as: :string %>
  <% end %>
<% elsif f.object.is_a?(CommunicationWorkOrder) %>
  <%= f.input :communication_method, as: :select, collection: CommunicationMethodOptions.all %>
  <%= f.input :initiator_role, as: :select, collection: InitiatorRoleOptions.all %>
  <%= f.input :processing_opinion, as: :select, collection: ProcessingOpinionOptions.all, include_blank: '无' %>
  <% if !f.object.new_record? && f.object.status.in?(%w[approved rejected]) %>
    <%= f.input :audit_result, input_html: { disabled: true } %>
    <%= f.input :audit_comment %>
    <%= f.input :audit_date, as: :string %>
  <% end %>
<% end %>

<% if f.object.reimbursement.present? %>
  <%= render 'admin/reimbursements/reimbursement_display', reimbursement: f.object.reimbursement %>
<% end %>
<script>
$(function() {
  // 问题类型变动时，加载问题说明和补充材料
  function loadProblemTypeRelated(problemTypeId, selectedDescId, selectedMaterials) {
    var $descSelect = $('.problem-description-select');
    var $materialSelect = $('.material-select');
    $descSelect.empty().append('<option value="">无</option>');
    $materialSelect.empty();
    if (!problemTypeId) return;
    // 拉取问题说明
    $.get('/admin/problem_descriptions.json', { q: { problem_type_id_eq: problemTypeId } }, function(data) {
      data.forEach(function(desc) {
        var selected = selectedDescId && desc.id == selectedDescId ? ' selected' : '';
        $descSelect.append('<option value="' + desc.id + '"' + selected + '>' + desc.description + '</option>');
      });
    });
    // 拉取补充材料
    $.get('/admin/problem_type_materials.json', { q: { problem_type_id_eq: problemTypeId }, include: 'material' }, function(data) {
      data.forEach(function(ptm) {
        if(ptm.material) {
          var selected = selectedMaterials && selectedMaterials.includes(ptm.material.id) ? ' selected' : '';
          $materialSelect.append('<option value="' + ptm.material.id + '"' + selected + '>' + ptm.material.name + '</option>');
        }
      });
    });
  }

  // 页面加载时自动预加载
  var initialProblemTypeId = $('.problem-type-select').val();
  var initialProblemDescriptionId = $('.problem-description-select').data('selected');
  if (initialProblemTypeId) {
    loadProblemTypeRelated(initialProblemTypeId, initialProblemDescriptionId, []);
  }

  $('.problem-type-select').on('change', function() {
    var problemTypeId = $(this).val();
    loadProblemTypeRelated(problemTypeId, null, []);
  });

  // 补充材料选中时的处理 - 备注字段已移除
  $('.material-select').on('change', function() {
    var selected = $(this).find('option:selected').map(function() { return $(this).text(); }).get();
    console.log('已选择补充材料:', selected.join('，'));
  });
});
</script> 